//
//  LSStatusViewModel.swift
//  MicroBlog
//
//  Created by lj on 15/9/9.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class LSStatusViewModel: NSObject {
    
    var status: LSStatus

    //userIconUrl
    var userIconUrl:NSURL? {
        return NSURL(string: status.user?.profile_image_url ?? "")
    }
    
    /// 配图 URL 数组
    var thumbnailURLs:[NSURL]?

    var bmiddleURLs:[NSURL]? {
        // 1. 判断 thumbnailURLs 是否为 nil
        guard let urls = thumbnailURLs else {
            return nil
        }
        
        // 2. 顺序替换每一个 url 字符串中的单词
        var array = [NSURL]()
        
        for url in urls {
            let urlString = url.absoluteString.stringByReplacingOccurrencesOfString("/thumbnail/", withString: "/bmiddle/")
            
            array.append(NSURL(string: urlString)!)
        }
        return array
    }
    
    
    //MARK: -构造函数
    
    init(status:LSStatus) {
    
        self.status = status
        
        //给配图数组设置数值
        if status.pic_urls != nil {
            thumbnailURLs = [NSURL]()
            
            for dict in status.pic_urls! {
                
                thumbnailURLs?.append(NSURL(string: dict["thumbnail_pic"]!)!)
            }
            
        }
        
        
        super.init()
    }
    
    
    override var description:String {
        return status.description + "缩略图 URL 数组\(thumbnailURLs)"
    }
    
    
}
